using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Common;
using Microsoft.TeamFoundation.WorkItemTracking;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.TeamFoundation.Controls;
using C1.Win.C1FlexGrid;

namespace Hemi
{
    public partial class Hemi : Form
    {
        HemiMovePipeline _pipeLine;
        ProgressDialog _progressDialog;
        Label _startLabel;
        UserControl _controlToRemove;
        
        public Hemi()
        {
            InitializeComponent();            
            
            _progressDialog = new ProgressDialog();
            _progressDialog.StartPosition = FormStartPosition.CenterScreen;

            this.StartPosition = FormStartPosition.CenterScreen;

            _hostTableLayout.Visible = false;

            _moveWorkItemsToolStripMenuItem.Enabled = true;
            _nextButton.Enabled = false;
            _cancelButton.Enabled = false;

            _startLabel = new Label();
            _startLabel.Text = "Use the File menu to move Work Items";
            _startLabel.Dock = DockStyle.Fill;            
            _hostPanel.Controls.Add(_startLabel);
        }
       
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void moveWorkItemsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StartPipeLine();           
        }

        private void StartPipeLine()
        {
            this.SuspendLayout();
            _cancelButton.Enabled = true;
            _nextButton.Enabled = true;
            _moveWorkItemsToolStripMenuItem.Enabled = false;
            _startLabel.Visible = false;
            _hostTableLayout.Visible = true;

            if (_controlToRemove != null)
            {
                _hostTableLayout.Controls.Remove(_controlToRemove);
                _controlToRemove = null;
            }
            
            this.ResumeLayout();

            _pipeLine = new HemiMovePipeline();           
            _hostTableLayout.Controls.Add(_pipeLine.CurrentStage.UserControl, 0, 1);

            _descriptionTextbox.Text = _pipeLine.CurrentStage.Description;
            _validateButton.Enabled = _pipeLine.CurrentStage.CanTestValidate;            
        }

        private void _nextButton_Click(object sender, EventArgs e)
        {
            //
            // Move to the next stage
            //
            _hostTableLayout.Controls.Remove(_pipeLine.CurrentStage.UserControl);
            _pipeLine.MoveNextStage();

            _hostTableLayout.Controls.Add(_pipeLine.CurrentStage.UserControl, 0, 1);
            _descriptionTextbox.Text = _pipeLine.CurrentStage.Description;

            _validateButton.Enabled = _pipeLine.CurrentStage.CanTestValidate;

            //
            // See if we have any more stages
            //
            bool hasMore = _pipeLine.PeekHasMore();

            if (!hasMore)
            {
                _cancelButton.Enabled = false;
                _nextButton.Enabled = false;
                _moveWorkItemsToolStripMenuItem.Enabled = true;
                _controlToRemove = _pipeLine.CurrentStage.UserControl;
            }
        }

        private void _validateButton_Click(object sender, EventArgs e)
        {
            if (!_pipeLine.CurrentStage.Validate())
            {
                MessageBox.Show(_pipeLine.CurrentStage.InvalidMessage, "Validation Errors");
            }
            else
            {
                MessageBox.Show(_pipeLine.CurrentStage.ValidMessage);
            }
        }

        private void _cancelButton_Click(object sender, EventArgs e)
        {
            //
            // Stop everything
            //
            DialogResult result = MessageBox.Show("Are you sure you want to cancel?", "Confirm", MessageBoxButtons.YesNo);

            if (result == DialogResult.Yes)
            {
                //
                // Cancel it all
                //
                _pipeLine = null;

                _moveWorkItemsToolStripMenuItem.Enabled = true;
                _nextButton.Enabled = false;
                _cancelButton.Enabled = false;
                _hostTableLayout.Visible = false;
                _startLabel.Visible = true;                
            }
        }

        private void _loadBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            StartPipeLine();
        }

        private void _loadBackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            SafeUpdateProgress(e);
        }

        private void _loadBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            _progressDialog.Close();
        }

        private void SafeUpdateProgress(ProgressChangedEventArgs e)
        {
            _progressDialog._progressLabel.Text = e.UserState.ToString();

            int potentialUpdate = _progressDialog._progressBar.Value + e.ProgressPercentage;
            if (potentialUpdate < 100)
            {
                _progressDialog._progressBar.Value += e.ProgressPercentage;
            }
        }
    }    
}